package database;

import shopping.*;

import java.sql.*;
import java.io.*;
import java.util.*;
public class GetData {
	 public static Produce getProduceById(long produceId)
	 {
		 Produce produce = null;
		 Connection con = new Connecter().getConnenction();
		 Statement stat = null;
		 ResultSet res = null;
		 String sqlQuery = "select * from produce " +
		  "where produceId = " +produceId +" order by produceId";
		// System.out.println( "getProduceById  --> "+sqlQuery);
		 try
		 {
			if(con == null)
			{
				return null;
			}
			stat = con.createStatement();
			if(stat == null)
			{
				return null;
			}
			res = stat.executeQuery(sqlQuery);
			if (res.next())
			{
				produce = new Produce(res.getLong("produceId"));
				produce.setProduceName(res.getString("produceName"));
				produce.setMainInfo(res.getString("mainInfo"));
				produce.setPrice(res.getDouble("producePrice"));
				produce.setLeftCount(res.getInt("leftCount"));
				produce.setImagePath(res.getString("imagePath"));
				produce.setType(res.getInt("produceType"));
				produce.setPublisher(res.getString("publisher"));
			}
		 }
		 catch(SQLException e)
		 {
			 e.printStackTrace();
		 }
		 return produce;
	 }
 public static Produce getProduceById(String produceId)
 {
	 long prodId = Long.parseLong(produceId);
	 return getProduceById(prodId);
 }
 
 public static Iterator<Produce> getProduceByName(String produceName)
{ 
	 Produce produce = null;
	 Iterator<Produce> iter = null;
	 HashSet set = new HashSet();
	 Connection con = new Connecter().getConnenction();
	 Statement stat = null;
	 ResultSet res = null;
	 String sqlQuery = "select * from produce  where produceName = '"+produceName+"'"+" order by produceId";
	// System.out.println("getProduceByName--> " + sqlQuery);
	 try
	 {
		stat = con.createStatement();
		res = stat.executeQuery(sqlQuery);
			while(res.next())
			{  
				produce = new Produce(res.getLong("produceId"));
				produce.setProduceName(res.getString("produceName"));
				produce.setMainInfo(res.getString("mainInfo"));
				produce.setPrice(res.getDouble("producePrice"));
				produce.setLeftCount(res.getInt("leftCount"));
				produce.setImagePath(res.getString("imagePath"));
				produce.setType(res.getInt("produceType"));
				produce.setPublisher(res.getString("publisher"));
 				set.add(produce);
			}
	 }
	 catch(SQLException e)
	 {
		 e.printStackTrace();
	 }
	 iter = set.iterator();
	 return iter;
 }
 public static Iterator<Produce> getProduceByType(long produceType)
 { 
 	 Produce produce = null;
 	 Iterator<Produce> iter = null;
 	 HashSet set = new HashSet();
 	 Connection con = new Connecter().getConnenction();
 	 Statement stat = null;
 	 ResultSet res = null;
 	 String sqlQuery = "select * from produce " ;
 		 if (produceType == 0)
 		 {
 		 }else
 		 {
 			sqlQuery += "where produceType = " +produceType ;
 		 }
 	sqlQuery +=
 		 " order by produceId";
 	//System.out.println( "getProduceByType -->" +sqlQuery);
 	 try
 	 {
 		 stat = con.createStatement();
 		res = stat.executeQuery(sqlQuery);
 			while(res.next())
 			{
 				produce = new Produce(res.getLong("produceId"));
				produce.setProduceName(res.getString("produceName"));
				produce.setMainInfo(res.getString("mainInfo"));
				produce.setPrice(res.getDouble("producePrice"));
				produce.setLeftCount(res.getInt("leftCount"));
				produce.setImagePath(res.getString("imagePath"));
				produce.setType(res.getInt("produceType"));
				produce.setPublisher(res.getString("publisher"));
 				set.add(produce);
 			}
 	 }
 	 catch(SQLException e)
 	 {
 		 e.printStackTrace();
 	 }
 	 catch(Exception e)
 	 {
 		 e.printStackTrace();
 	 }
 	 iter = set.iterator();
 	 return iter;
  }
 
 public static Iterator<Produce> getProduceByType(String type)
 {
	 if (type == null) type = "0";
	 return getProduceByType(Long.parseLong(type));
 }
 public static Customer hasCustomer(String id,String pwd)
 {
	 if ( ( id == null || id.length() < 1) && (pwd == null || pwd .length() < 1))
	 {
		 return null;
	 }
	 Connection con = new Connecter().getConnenction();
	 Statement stat = null;
	 ResultSet res = null;
	 Customer customer = null;
	 try
	 {
		 id = new String( id.getBytes("iso-8859-1"));
	 }
	 catch(Exception e)
	 {
		 e.printStackTrace();
	 }
	 String sqlQuery = "select customerId from customer " +
	     "where customerId = '" + id +"' and  customerPwd='"+pwd+"'";
	// System.out.println("hasCustomer--> " + sqlQuery);
	 try
	 {
		 stat = con.createStatement();
		 res = stat.executeQuery(sqlQuery);
		 if(res.next())
		 { 
			 customer = new Customer(id);
		 }
		 return customer;
	 }
	 catch(SQLException e )
	 {
		 e.printStackTrace();
	 }
	 return customer;
 }
 
 public static Manager  hasManager(String id,String pwd)
 {
	 if ( ( id == null || id.length() < 1) && (pwd == null || pwd .length() < 1))
	 {
		 return null;
	 }
	 try
	 {
		 id = new String(id.getBytes("iso-8859-1"));	 
	 }
	 catch(Exception e)
	 {
		 e.printStackTrace();
	 }
	
	 String sqlString = "select *  from manager where id ='"+id+"' and pwd='"+pwd+"'";
	 System.out.println("in hasManager: " + sqlString);
	 Statement state = null;
	 try
	 {
		 state = new Connecter().getConnenction().createStatement();;
		 ResultSet res = state.executeQuery(sqlString);
		 if (res.next())
		 {
			 return new Manager(id);
		 }
	 }
	 catch(SQLException e)
	 {
		 
	 }
	 return null;
 } 
 
 public static Order getOrder(String produceId,int state,String customerId)
 {
	 if (produceId == null) return null;
	 if (customerId == null ) return null;
	 Order order =  null;
	 Connection con = new Connecter().getConnenction();
	  Statement stat = null;
	  ResultSet res = null;
	  String sqlQuery ="select * from orders where produceId="+produceId +" and ownerId='"+customerId+"'" ;
	  sqlQuery +=" order by produceId";
	 // System.out.println("getOrder--> " +sqlQuery);
	  try
	  {
		  stat =  con.createStatement();
		  res = stat.executeQuery(sqlQuery);
		  if (res.next())
		  {
			  order = new Order(res.getString("ownerId"));
			  order.setOrderState(res.getInt("orderState"));
			  order.setProduceId(res.getLong("produceId"));
			  order.setCount(res.getInt("produceCount"));
			  System.out.println(order.getProduceName());
		  }
	  }
	  catch(SQLException e)
	  {
		  e.printStackTrace();
	  }
	 return order;
 }
 public static Iterator<Order> getOrders(Customer customer,int state)
 {   
	 if (customer == null) return null;
	 String ownerId = customer.getCustomerId();
	 if (ownerId == null)
	 {
		 return null;
	 }
	  Order order =  null;
	  HashSet set = new HashSet();
	  Connection con = new Connecter().getConnenction();
	  Statement stat = null;
	  ResultSet res = null;
	  String sqlQuery = "select * from orders where ownerId = '" + customer.getCustomerId()+"'and orderState="+state+ " order by produceId";
	//  System.out.println("getOrders---> " + sqlQuery);
	  
	  try
	  {
		  stat =  con.createStatement();
		  res = stat.executeQuery(sqlQuery);
		  System.out.println(res.getRow());
		  while(res.next())
		  {
			  order = new Order(res.getString("ownerId"));
			  order.setOrderState(res.getInt("orderState"));
			  order.setProduceId(res.getLong("produceId"));
			  order.setCount(res.getInt("produceCount"));
			  set.add(order);
		  }
	  }
	  catch(SQLException e)
	  {
		  e.printStackTrace();
	  }
	 return set.iterator();
 }
 
 public static Iterator<Type> getAllType()
 {
	 Connection con = new Connecter().getConnenction();
	 String sqlQuery = "select * from produceType  order by typeId";
	// System.out.println("getAllType ---> "+sqlQuery);
	 Statement sta = null;
	 ResultSet res = null;
	 HashSet set = new HashSet();
	 try
	 {
		 sta = con.createStatement();
		 res = sta.executeQuery(sqlQuery);
		 Type type = null;
		 while (res.next())
		 {
			 type = new Type( res.getLong("typeId"));
			 type.setTypeName(res.getString("typeName"));
			 set.add(type);
			 
		 }
	 }catch(SQLException e)
	 {
		 e.printStackTrace();
	 }
	 return set.iterator();
 }
}
